package com.ccys.cms.service.thinkWell.impl;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.thinkWell.TblPhqDetailWebService;
import com.ccys.cms.service.thinkWell.TblPhqScoringWebService;
import com.ccys.cms.service.thinkWell.TblPhqWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.thinkWell.TblPhqDTO;
import com.ccys.db.dto.thinkWell.TblPhqDetailDTO;
import com.ccys.db.entity.TblPhq;
import com.ccys.db.entity.TblPhqDetailResponse;
import com.ccys.db.entity.TblPhqScoring;
import com.ccys.db.mapper.TblPhqDetailResponseMapper;
import com.ccys.db.mapper.TblPhqMapper;
import com.ccys.db.mapper.TblPhqScoringMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.thinkWell.TblPhqDetailWebVO;
import com.ccys.db.vo.thinkWell.TblPhqWebVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * tbl_phq管理
 */
@Service
@Slf4j
public class TblPhqWebServiceImpl extends BaseServiceImpl<TblPhq> implements TblPhqWebService {

    @Autowired(required = false)
    private TblPhqMapper tblPhqMapper;
    @Autowired(required = false)
    private TblPhqScoringMapper tblPhqScoringMapper;

    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Autowired(required = false)
    private TblPhqDetailResponseMapper tblPhqDetailResponseMapper;
    @Autowired(required = false)
    private TblPhqDetailWebService tblPhqDetailWebService;
    @Autowired(required = false)
    private TblPhqScoringWebService tblPhqScoringWebService;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblPhqDTO dto) {
        PageUtils.startPage(request);
        List<TblPhqWebVO> list = this.tblPhqMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(TblPhqDTO entity) {
        String id = "";
        if (StringUtils.isBlank(entity.getId())) {
            if(entity == null) {
                entity.setStatus(1);
            }
            // 新增
            this.saveEntity(entity);
            id = entity.getId();
            //initScoringData(id);

        } else {
            // 编辑
            this.updateEntityById(entity);
            id = entity.getId();
            if(entity.getStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getStatus());
            }
        }
        //处理  PHQ4 questions
        List<TblPhqDetailDTO> tblPhqDetailDTOList = entity.getTblPhqDetailDTOList();
        if(tblPhqDetailDTOList != null && !tblPhqDetailDTOList.isEmpty()) {
            for (TblPhqDetailDTO detailDTO : tblPhqDetailDTOList) {
                if(StringUtil.isEmpty(detailDTO.getId())) {
                    if(detailDTO.getStatus() == null) {
                        detailDTO.setStatus(1);
                    }
                    // 新增
                    this.tblPhqDetailWebService.saveEntity(detailDTO);
                    List<TblPhqDetailResponse> responseList = detailDTO.getResponseList();
                    try {
                        for (TblPhqDetailResponse r : responseList) {
                            r.setDetailId(entity.getId());
                            r.setCreatedOnUtc(System.currentTimeMillis());
                            r.setCreatedBy(UserUtil.getCurLoginUserId());
                            r.setId(IdUtil.randomUUID());
                            r.setDelFlag(0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    this.tblPhqDetailResponseMapper.batchInsert(responseList);
                }else {
                    // 编辑
                    this.tblPhqDetailWebService.updateEntityById(detailDTO);
                    if(detailDTO.getStatus() != null) {
                        this.baseTblAuditTrailWebService.saveStatusLog(detailDTO,detailDTO.getStatus());
                    }
                    TblPhqDetailResponse tblPhqDetailResponse = new TblPhqDetailResponse();
                    tblPhqDetailResponse.setDetailId(detailDTO.getId());
                    this.tblPhqDetailResponseMapper.delete(tblPhqDetailResponse);
                    List<TblPhqDetailResponse> responseList = detailDTO.getResponseList();
                    try {
                        for (TblPhqDetailResponse r : responseList) {
                            r.setDetailId(entity.getId());
                            r.setCreatedOnUtc(System.currentTimeMillis());
                            r.setCreatedBy(UserUtil.getCurLoginUserId());
                            r.setId(IdUtil.randomUUID());
                            r.setDelFlag(0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    this.tblPhqDetailResponseMapper.batchInsert(responseList);
                }

            }

        }
        //处理 Scoring logic
        List<TblPhqScoring> tblPhqScoringList = entity.getTblPhqScoringList();
        if(tblPhqScoringList != null && !tblPhqDetailDTOList.isEmpty()) {
            for (TblPhqScoring t : tblPhqScoringList) {
                if(StringUtil.isEmpty(t.getId())) {
                    t.setPhqId(id);
                    this.tblPhqScoringWebService.saveEntity(t);
                }else{
                    this.tblPhqScoringWebService.updateEntityById(t);
                }
            }
        }
        return ResponseResult.success();
    }


    private void initScoringData(String phqId) {
        //初始化 Scoring logic 数据
        String scoringType = "1,2,3,4";
        try {
            for (String t : scoringType.split(",")) {
                TblPhqScoring scoring = new TblPhqScoring();
                scoring.setPhqId(phqId);
                scoring.setScoringType(Integer.parseInt(t));
                scoring.setCreatedOnUtc(System.currentTimeMillis());
                scoring.setCreatedBy(UserUtil.getCurLoginUserId());
                scoring.setId(IdUtil.randomUUID());
                scoring.setDelFlag(0);
                this.tblPhqScoringMapper.insert(scoring);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ResponseResult delete(TblPhq dto) {
        TblPhq entity = new TblPhq();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        this.updateEntityById(entity);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult detail(HttpServletRequest request, HttpServletResponse response, TblPhqDTO dto) {
        TblPhqDetailWebVO tblPhqDetailWebVO = this.tblPhqMapper.getDetail(dto);
        return ResponseResult.success(tblPhqDetailWebVO);
    }


}